package com.jams.goodox.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.NumberPicker.OnValueChangeListener;

import com.jams.goodox.R;
import com.jams.goodox.utils.DayUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class BirthdayDateTimePicker extends FrameLayout {
	private final NumberPicker mMonthSpinner;
	private final NumberPicker mDaySpinner;
	private final NumberPicker mYearSpinner;
	private OnYearChangedListener onYearChangedListener;
	
	public BirthdayDateTimePicker(Context context, int from, int year, int month, int day) {
		super(context);
		
		String yearMonth = getCurrentDate();
		/**
		 * 加载布局
		 */
		inflate(context, R.layout.birthdaydatedialog, this);
		/**
		 * 初始化控件
		 */
		mMonthSpinner = (NumberPicker) this.findViewById(R.id.np_month);
		mMonthSpinner.setFocusable(true);
		mMonthSpinner.setFocusableInTouchMode(true);
		mMonthSpinner.setMinValue(1);
		mMonthSpinner.setMaxValue(12);
		
		mMonthSpinner.setOnValueChangedListener(mOnMonthTimeChangedListener);
		
		EditText dataInput = findInput(mMonthSpinner);
		dataInput.setFocusable(false);
		
		mDaySpinner = (NumberPicker) this.findViewById(R.id.np_day);
		mDaySpinner.setFocusable(true);
		mDaySpinner.setFocusableInTouchMode(true);
		mDaySpinner.setMaxValue(DayUtils.getDay(year, month + ""));
		mDaySpinner.setMinValue(1);
		mDaySpinner.setValue(DayUtils.setDay);
		mDaySpinner.setOnValueChangedListener(mOnDayChangedListener);
		EditText dayInput = findInput(mDaySpinner);
		dayInput.setFocusable(false);
		
		mYearSpinner = (NumberPicker) this.findViewById(R.id.np_year);
		mYearSpinner.setFocusable(true);
		mYearSpinner.setFocusableInTouchMode(true);
		mYearSpinner.setMaxValue(DayUtils.year);
		
		if (from == 1) {
			mYearSpinner.setValue(Integer.valueOf(yearMonth.split("-")[0]));
			mYearSpinner.setMinValue(2013);
			mMonthSpinner.setValue(Integer.valueOf(yearMonth.split("-")[1]));
		} else {
			mYearSpinner.setValue(DayUtils.setYear);
			mYearSpinner.setMinValue(1900);
			mMonthSpinner.setValue(DayUtils.setMonth);
		}
		
		mYearSpinner.setOnValueChangedListener(mOnYearChangedListener);
		EditText hourInput = findInput(mDaySpinner);
		hourInput.setFocusable(false);
		
		if (from == 1) {
			mDaySpinner.setVisibility(View.GONE);
		}
	}
	
	/**
	 * 获取当前日期
	 * 
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public String getCurrentDate() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String newDate = sdf.format(date);
		return newDate;
	}
	
	/**
	 * find spinner的子类eittext，禁止输入数据
	 * 
	 * @param np
	 * @return
	 */
	private EditText findInput(ViewGroup np) {
		int count = np.getChildCount();
		for (int i = 0; i < count; i++) {
			final View child = np.getChildAt(i);
			if (child instanceof ViewGroup) {
				findInput((ViewGroup) child);
			} else if (child instanceof EditText) {
				return (EditText) child;
			}
		}
		return null;
	}
	
	/**
	 * 
	 * 控件监听器 注意onvaluechange里面的顺序不同，如果改变顺序会造成索引角标越界的bug
	 */
	private OnValueChangeListener mOnMonthTimeChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			mDaySpinner.setMaxValue(DayUtils.getDay(DayUtils.year, newVal + ""));
			mDaySpinner.setMinValue(1);
			/**
			 * 给接口传值
			 */
			onDateTimeChanged();
		}
	};

	private OnValueChangeListener mOnDayChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			onDateTimeChanged();
		}
	};

	private OnValueChangeListener mOnYearChangedListener = new OnValueChangeListener() {
		@Override
		public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
			mDaySpinner.setMaxValue(DayUtils.getDay(mYearSpinner.getValue(), String.valueOf(mMonthSpinner.getValue())));
			mDaySpinner.setMinValue(1);
			onDateTimeChanged();
		}
	};
	
	/*
	 * 接口回调 参数是当前的View 年月日小时分钟
	 */
	public interface OnYearChangedListener {
		void onDateTimeChanged(BirthdayDateTimePicker view, int year, int month, int day);
	}
	
	/*
	 * 对外的公开方法
	 */
	public void setOnYearChangedListener(OnYearChangedListener callback) {
		onYearChangedListener = callback;
	}
	
	private void onDateTimeChanged() {
		if (onYearChangedListener != null) {
			onYearChangedListener.onDateTimeChanged(this, (mYearSpinner.getValue()), (mMonthSpinner.getValue()),
					(mDaySpinner.getValue()));
		}
	}
}
